package com.huawei.leetcode;

import java.util.Objects;

public class Code2734 {

    public static void main(String[] args) {
        String s = "acbbc";
        System.out.println(smallestString(s));
    }

    public static String smallestString(String s) {
        String[] subStr = s.split("a");
        if (subStr.length == 0) {
            if (s.length() == 1) {
                return "z";
            } else {
                return s.substring(0,s.length() - 1) + 'z';
            }
        }
        String max_sub = "";
        for (String s1 : subStr) {
            if(s1.length()>0){
                max_sub = s1;
                break;
            }
        }
        int i = s.indexOf(max_sub);
        char[] chars = s.toCharArray();
        for (int j = i; j < i + max_sub.length(); j++) {
            if (Objects.equals('a', chars[j])) {
                chars[j] = 'z';
            } else {
                chars[j] = (char) (chars[j] - 1);
            }
        }
        return String.valueOf(chars);
    }
}
